local my_ctld = CTLD:New(SIDE.BLUE, { "BLUE", "Phase", "Khaimah", "Fujairah", "CVN", "KUZ" }, "Transport")

my_ctld:__Start(5)

my_ctld:AddTroopsCargo("反坦克部队", { "Blue AT Force" }, CTLD_CARGO.Enum.TROOPS, 11)

my_ctld:AddTroopsCargo("工程师", { "Engineer" }, CTLD_CARGO.Enum.ENGINEERS, 1)

my_ctld.EngineerSearch = 2000

my_ctld:AddCratesCargo("IFV小组(550kg * 2)", { "IFV" }, CTLD_CARGO.Enum.VEHICLE, 2, 550)

my_ctld:AddCratesCargo("MBT(600kg * 2)", { "MBT" }, CTLD_CARGO.Enum.VEHICLE, 2, 600)

my_ctld:AddCratesCargo("补给车(200kg * 1)", { "AmmoCar" }, CTLD_CARGO.Enum.VEHICLE, 1, 200)

my_ctld:AddCratesCargo("自行火炮小队(搭载核弹头) (450 * 3)", { "Arty" }, CTLD_CARGO.Enum.VEHICLE, 3, 450)

my_ctld:AddCratesCargo("FARP(300kg * 2)", { "FOB" }, CTLD_CARGO.Enum.FOB, 2, 300)

my_ctld:AddCTLDZone("BLUE TransShip-1", CTLD.CargoZoneType.SHIP, SMOKECOLOR.Blue, true, true, 240, 20)
my_ctld:AddCTLDZone("BLUE TransShip-2", CTLD.CargoZoneType.SHIP, SMOKECOLOR.Blue, true, true, 240, 20)
my_ctld:AddCTLDZone("BLUE TransShip-3", CTLD.CargoZoneType.SHIP, SMOKECOLOR.Blue, true, true, 240, 20)
my_ctld:AddCTLDZone("BLUE TransShip-5", CTLD.CargoZoneType.SHIP, SMOKECOLOR.Blue, true, true, 240, 20)
my_ctld:AddCTLDZone("LOADZONETEST", CTLD.CargoZoneType.LOAD, SMOKECOLOR.Blue, true, true)
my_ctld:AddCTLDZone("DROPZONE", CTLD.CargoZoneType.DROP, SMOKECOLOR.GREEN, true, true)
my_ctld:AddCTLDZone("MOVEZONE-1", CTLD.CargoZoneType.MOVE, SMOKECOLOR.Orange, true, true)
my_ctld:AddCTLDZone("MOVEZONE-2", CTLD.CargoZoneType.MOVE, SMOKECOLOR.Orange, true, true)
my_ctld:AddCTLDZone("MOVEZONE-3", CTLD.CargoZoneType.MOVE, SMOKECOLOR.Orange, true, true)
my_ctld:AddCTLDZone("MOVEZONE-4", CTLD.CargoZoneType.MOVE, SMOKECOLOR.Orange, true, true)
my_ctld:AddCTLDZone("KhaimahZone", CTLD.CargoZoneType.LOAD, SMOKECOLOR.Orange, true, true)

my_ctld.useprefix = true             -- (DO NOT SWITCH THIS OFF UNLESS YOU KNOW WHAT YOU ARE DOING!) Adjust **before** starting CTLD. If set to false, *all* choppers of the coalition side will be enabled for CTLD.
my_ctld.CrateDistance = 500         -- List and Load crates in this radius only.
my_ctld.dropcratesanywhere = false   -- Option to allow crates to be dropped anywhere.
my_ctld.dropAsCargoCrate = false     -- Parachuted herc cargo is not unpacked automatically but placed as crate to be unpacked. Needs a cargo with the same name defined like the cargo that was dropped.
my_ctld.maximumHoverHeight = 15      -- Hover max this high to load.
my_ctld.minimumHoverHeight = 4       -- Hover min this low to load.
my_ctld.forcehoverload = false       -- Crates (not: troops) can **only** be loaded while hovering.
my_ctld.hoverautoloading = true      -- Crates in CrateDistance in a LOAD zone will be loaded automatically if space allows.
my_ctld.smokedistance = 2000         -- Smoke or flares can be request for zones this far away (in meters).
my_ctld.movetroopstowpzone = true    -- Troops and vehicles will move to the nearest MOVE zone...
my_ctld.movetroopsdistance = 500000  -- .. but only if this far away (in meters)
my_ctld.smokedistance = 2000         -- Only smoke or flare zones if requesting player unit is this far away (in meters)
my_ctld.suppressmessages = false     -- Set to true if you want to script your own messages.
my_ctld.repairtime = 300             -- Number of seconds it takes to repair a unit.
my_ctld.buildtime = 10               -- Number of seconds it takes to build a unit. Set to zero or nil to build instantly.
my_ctld.cratecountry = country.id
    .GERMANY                         -- ID of crates. Will default to country.id.RUSSIA for RED coalition setups.
my_ctld.allowcratepickupagain = true -- allow re-pickup crates that were dropped.
my_ctld.enableslingload = false      -- allow cargos to be slingloaded - might not work for all cargo types
my_ctld.pilotmustopendoors = false   -- force opening of doors
my_ctld.SmokeColor = SMOKECOLOR
    .Red                             -- default color to use when dropping smoke from heli
my_ctld.FlareColor = FLARECOLOR
    .Red                             -- color to use when flaring from heli
my_ctld.basetype =
"container_cargo"                    -- default shape of the cargo container
my_ctld.droppedbeacontimeout = 600   -- dropped beacon lasts 10 minutes
my_ctld.usesubcats = false           -- use sub-category names for crates, adds an extra menu layer in "Get Crates", useful if you have > 10 crate types.
my_ctld.placeCratesAhead = false     -- place crates straight ahead of the helicopter, in a random way. If true, crates are more neatly sorted.
my_ctld.nobuildinloadzones = true    -- forbid players to build stuff in LOAD zones if set to `true`
my_ctld.movecratesbeforebuild = true -- crates must be moved once before they can be build. Set to false for direct builds.
my_ctld.surfacetypes = { land.SurfaceType.LAND, land.SurfaceType.ROAD, land.SurfaceType.RUNWAY,
  land.SurfaceType.SHALLOW_WATER }   -- surfaces for loading back objects.
my_ctld.nobuildmenu = false          -- if set to true effectively enforces to have engineers build/repair stuff for you.
my_ctld.enableHercules = true
my_ctld.HercMinAngels = 155          -- for troop/cargo drop via chute in meters, ca 470 ft
my_ctld.HercMaxAngels = 2000         -- for troop/cargo drop via chute in meters, ca 6000 ft
my_ctld.HercMaxSpeed = 77            -- 77mps or 270kph or 150kn

my_ctld:UnitCapabilities("UH-60L", true, true, 3, 20, 16, 3600)



-- FARP Radio. First one has 130AM, next 131 and for forth
local FARPFreq = 130
local FARPName = 1 -- numbers 1..10

local FARPClearnames = {
  [1] = "London",
  [2] = "Dallas",
  [3] = "Paris",
  [4] = "Moscow",
  [5] = "Berlin",
  [6] = "Rome",
  [7] = "Madrid",
  [8] = "Warsaw",
  [9] = "Dublin",
  [10] = "Perth",
}

function BuildAFARP(Coordinate)
  local coord = Coordinate -- Core.Point#COORDINATE

  local FarpName = ((FARPName - 1) % 10) + 1
  local FName = FARPClearnames[FarpName]

  FARPFreq = FARPFreq + 1
  FARPName = FARPName + 1

  -- Create a SPAWNSTATIC object from a template static FARP object.
  local SpawnStaticFarp = SPAWNSTATIC:NewFromStatic("Static FARP-1", country.id.CJTF_BLUE)

  -- Spawning FARPs is special in DCS. Therefore, we need to specify that this is a FARP. We also set the callsign and the frequency.
  SpawnStaticFarp:InitFARP(FARPName, FARPFreq, 0)
  SpawnStaticFarp:InitDead(false)

  -- Spawn FARP
  local ZoneSpawn = ZONE_RADIUS:New("FOB " .. FName, Coordinate:GetVec2(), 160, false)
  local Heading = 0
  local FarpBerlin = SpawnStaticFarp:SpawnFromZone(ZoneSpawn, Heading, "FARP " .. FName)

  -- ATC and services - put them 125m from the center of the zone towards North
  local FarpVehicles = SPAWN:NewWithAlias("FARP Vehicles Template", "FOB " .. FName .. " Technicals")
  FarpVehicles:InitHeading(180)
  local FarpVCoord = coord:Translate(125, 0)
  FarpVehicles:SpawnFromCoordinate(FarpVCoord)


  -- We will put the rest of the statics in a nice circle around the center
  local base = 330
  local delta = 30

  local windsock = SPAWNSTATIC:NewFromStatic("Static Windsock-1-1", country.id.CJTF_BLUE)
  local sockcoord = coord:Translate(125, base)
  windsock:SpawnFromCoordinate(sockcoord, Heading, "Windsock " .. FName)
  base = base - delta

  local fueldepot = SPAWNSTATIC:NewFromStatic("Static FARP Fuel Depot-1", country.id.CJTF_BLUE)
  local fuelcoord = coord:Translate(125, base)
  fueldepot:SpawnFromCoordinate(fuelcoord, Heading, "Fueldepot " .. FName)
  base = base - delta

  local ammodepot = SPAWNSTATIC:NewFromStatic("Static FARP Ammo Dump Coating-1-1", country.id.CJTF_BLUE)
  local ammocoord = coord:Translate(125, base)
  ammodepot:SpawnFromCoordinate(ammocoord, Heading, "Ammodepot " .. FName)
  base = base - delta

  local CommandPost = SPAWNSTATIC:NewFromStatic("Static FARP CP Blindage-1", country.id.CJTF_BLUE)
  local CommandCoord = coord:Translate(125, base)
  CommandPost:SpawnFromCoordinate(CommandCoord, Heading, "Command Post " .. FName)
  base = base - delta


  local Tent1 = SPAWNSTATIC:NewFromStatic("Static FARP Tent-1", country.id.CJTF_BLUE)
  local Tent1Coord = coord:Translate(125, base)
  Tent1:SpawnFromCoordinate(Tent1Coord, Heading, "Command Tent " .. FName)
  base = base - delta


  local Tent2 = SPAWNSTATIC:NewFromStatic("Static FARP Tent-1", country.id.CJTF_BLUE)
  local Tent2Coord = coord:Translate(125, base)
  Tent2:SpawnFromCoordinate(Tent2Coord, Heading, "Command Tent2 " .. FName)

  -- add a loadzone to CTLD
  my_ctld:AddCTLDZone("FOB " .. FName, CTLD.CargoZoneType.LOAD, SMOKECOLOR.Blue, true, true)

  MESSAGE:New(string.format("FARP %s in operation!", FName), 15, "CTLD", false):ToAll()
end

function my_ctld:OnAfterCratesDropped(From, Event, To, Group, Unit, Cargotable)
  local playerName = Unit:GetPlayerName()
  local playerucid = NET:New():GetPlayerUCID(nil, playerName)

  local url = "http://localhost:8079/api/player/transport"
  local params = "ucid=" .. playerucid
  MESSAGE:New(SendRequest(url, params), 10, "战绩系统", false):ToUnit(Unit)
end

function my_ctld:OnAfterCratesBuild(From, Event, To, Group, Unit, Vehicle)
  if Unit:GetPlayerName() then
    local playerName = Unit:GetPlayerName()
    local playerucid = NET:New():GetPlayerUCID(nil, playerName)

    local url = "http://localhost:8079/api/player/transport"
    local params = "ucid=" .. playerucid
    MESSAGE:New(SendRequest(url, params), 10, "战绩系统", false):ToUnit(Unit)
  end


  local name = Vehicle:GetName()
  if string.match(name, "FOB", 1) then
    local Coord = Vehicle:GetCoordinate()
    Vehicle:Destroy(false)
    BuildAFARP(Coord)
  end

  if string.match(name, "Arty", 1) then
    -- MessageToAll("ARTY is running!", 10, "DEBUG")
    local tempArty = ARTY:New(Vehicle)
    tempArty:SetTacNukeShells(1)
    tempArty:SetTacNukeWarhead(5)
    tempArty:SetReportON()
    tempArty:SetDebugOFF()
    tempArty:SetMarkAssignmentsOn(nil, true)
    tempArty:Start()
    -- MessageToAll("ARTY done", 10, "DEBUG")
  end
end
